package org.UserHouse.Servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.UserHouse.Dao.HitemDao;
import org.UserHouse.Dao.Imp.HitemDaoImp;
import org.UserHouse.entity.Hitem;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author bin
 * @date 2024-12-18
 */
@WebServlet("/add.Hitem.do")
public class AddHitemServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        PrintWriter out = resp.getWriter();

        String Hname = req.getParameter("Hitemname");
        String HitemTid = req.getParameter("HitemType");
        String HitemPrice = req.getParameter("HitemPrice");
        String Hgarde = req.getParameter("Hitemgrade");
        String Hcontent = req.getParameter("Hitemcontent");
        //获取数组的值
        String[] Himg = req.getParameterValues("img");
        String img = "";
        //对数组的内容进行拆分
        // img = "1.png,2.png,"
        for (String s : Himg) {
            img = img + s;
            img = img + ",";
        }

        // img = "1.png,2.png"
        if (img.length() != 0) {
            img = img.substring(0, img.length() - 1);
        }

        HitemDao hitemDao = new HitemDaoImp();
        Hitem hitem = new Hitem();

        int Tid;
        Double Hprice;

        Tid = Integer.parseInt(HitemTid);
        Hprice = Double.parseDouble(HitemPrice);

        hitem.setHname(Hname);
        hitem.setTid(Tid);
        hitem.setTprice(Hprice);
        hitem.setHgrade(Hgarde);
        hitem.setHcontent(Hcontent);
        hitem.setHimg(img);

        int n = hitemDao.AddHitem(hitem);
        out.println(n);

        System.out.println(hitem);

        out.flush();
        out.close();

    }
}
